package com.joplus.housekeeper.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by tangdehao on 16/5/17.
 */
public class DateUtils {


    static String[] weeks = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
    static String[] weeks2 = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
//    public static Date longToDate(long currentTime, String formatType)
//            throws ParseException {
//
//        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
//        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
//        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
//        return date;
//    }

    // string类型转换为date类型
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    public static String longToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);

        return formatter.format(new Long(strTime));

    }


    // date类型转换为String类型
    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    public static String LoongDateToDateString(String longDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time = new Long(longDate);
        String d = format.format(time);
        Date date = format.parse(d);
        return dateToString(date, "yyyy-MM-dd HH:mm");
    }

    public static String LoongDateToDateMonth(String longDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Long time = new Long(longDate);
        String d = format.format(time);
        Date date = format.parse(d);
        return dateToString(date, "yyyy-MM-dd");
    }

    public static String LoongDateToDateMonth(long longDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        String d = format.format(longDate);
        Date date = format.parse(d);
        return dateToString(date, "yyyy-MM-dd");
    }

    public static long betweenDay(String toT) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format1 = format.format(date);


        try {
            Date dateTo = format.parse(toT);
            Date dateT = format.parse(format1);
            long l = dateTo.getTime() - dateT.getTime();
            long d = l / (24 * 60 * 60 * 1000);
            return d;

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 1;
    }

    public static Boolean judeOutOfDate(String d) {
        if (StringUtils.isEmpty(d)) {
            return false;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            return date.after(format.parse(d));
        } catch (ParseException e) {
            return false;
        }

    }

    public static int dayForWeek(String pTime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(pTime));
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    public static int dateForWeek(Date pTime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(pTime);
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    //星期一星期二
    public static String dayFroWeek2(String s) {
        int day = -1;
        try {
            day = dayForWeek(s);
        } catch (Exception e) {
            day = -1;
        }
        if (day > 0) {
            return weeks[day - 1];
        } else {
            return "";
        }
    }

    //返回周一周二
    public static String day4Week(String s) {
        int day = -1;
        try {
            day = dayForWeek(s);
        } catch (Exception e) {
            day = -1;
        }
        if (day > 0) {
            return weeks2[day - 1];
        } else {
            return "";
        }
    }

    public static String dayFroWeek2(Date s) {
        int day = -1;
        try {
            day = dateForWeek(s);
        } catch (Exception e) {
            day = -1;
        }
        if (day > 0) {
            return weeks[day - 1];
        } else {
            return "";
        }
    }


}
